<%@page import="com.bionic.banking.logic.DomainUtil"%>
<%@page import="com.bionic.banking.core.entity.User"%>
<%@page import="com.bionic.banking.core.entity.Address"%>
<%@page import="com.bionic.banking.logic.controller.AddressController"%>
<%@page import="com.bionic.banking.logic.controller.UserController"%>
<%@page import="com.bionic.banking.logic.controller.CustomerController"%>
<%@page import="com.bionic.banking.core.entity.Customer"%>
<%@page language="java"%>
<%
    //Retrieve parameters
    //user
    String login = (String) request.getParameter("login");
    String password = (String) request.getParameter("password");

    //customer
    Integer id = Integer.parseInt(request.getParameter("id"));
    String firstname = (String) request.getParameter("firstName");
    String lastname = (String) request.getParameter("lastName");
    String middlename = request.getParameter("middleName");
    Integer age = Integer.parseInt(request.getParameter("age"));
    Boolean gender = Boolean.parseBoolean(request.getParameter("gender"));
    String email = request.getParameter("email");
    String fax = (String) request.getParameter("fax");
    String phone = (String) request.getParameter("phone");

    //address parameters
    String postalCode = (String) request.getParameter("postalcode");
    String country = (String) request.getParameter("country");
    String region = (String) request.getParameter("region");
    String city = (String) request.getParameter("city");
    String street = (String) request.getParameter("street");
    String line1 = (String) request.getParameter("line1");
    String line2 = (String) request.getParameter("line2");
    try {
        //Use session beans to update entity 'customer' and related user & address
        CustomerController bean = new CustomerController();
        UserController userbean = new UserController();
        AddressController addressbean = new AddressController();

        //get customer entity and reset its properties
        Customer customer = bean.getDetails(id);
        customer.setFirstName(firstname);
        customer.setLastName(lastname);
        customer.setMiddleName(middlename);
        customer.setAge(age);
        //customer.setGender(gender);
        customer.setPhone(phone);
        customer.setFax(fax);
        customer.setEmail(email);
        String imageBuffStr = (String) request.getParameter("imageBuffer");        
        if (!imageBuffStr.equals("")) {
            byte[] newImageBytes = DomainUtil.stringToBuffer(imageBuffStr);
            customer.setImage(newImageBytes);//update image if new
        }

        //update entity in DB
        bean.update(customer);

        //Get user entity
        Integer userId = customer.getUserId();
        if (userId != null) {
            User user = userbean.getDetails(userId);
            user.setLogin(login);
            user.setPassword(password);
            //update entity in DB
            userbean.update(user);
        }

        //Get address entity
        Integer addressId = customer.getAddressId();
        if (addressId != null) {
            Address address = addressbean.getDetails(addressId);
            address.setCountry(country);
            address.setRegion(region);
            address.setPostalCode(postalCode);
            address.setCity(city);
            address.setStreet(street);
            address.setLine1(line1);
            address.setLine2(line2);
            //update entity in DB
            addressbean.update(address);
        }
        //redirect to list
        response.sendRedirect("../Controller?command=viewCustomersList");
    } catch (Exception e) {
        System.out.println(e);
    }
%>